//
//  UserManager.m
//  MingShi
//
//  Created by MJiao on 15/7/1.
//  Copyright (c) 2015年 MJiao. All rights reserved.
//

#import "UserManager.h"

@implementation UserManager

+ (id)shareUserManager{
    static UserManager *sharedData = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedData = [[self alloc] init];
        
       User*  curUser = [[User alloc]init];
         //默认情况下当前登录的用户的userId为-1
        curUser.userId = -1;
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        curUser.cacheUserId = [userDefaults integerForKey:@"cacheUserId"];


        sharedData.curUser = curUser;
        //默认情况下bdPushChannelId为空；
        sharedData.bdPushChannelId = @"";
    });
    return sharedData;
}

- (void) setAutoLogin:(BOOL)isAutoLogin{
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:isAutoLogin forKey:@"isAutoLogin"];
    //这里建议同步存储到磁盘中
    [userDefaults synchronize];
}

- (void)saveUserToDisk:(User *)user {
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:user.account forKey:@"account"];
    [userDefaults setObject:user.pwd forKey:@"pwd"];
    [userDefaults setObject:user.openId forKey:@"openId"];
    [userDefaults setObject:user.accessToken forKey:@"accessToken"];

    self.curUser.cacheUserId = user.userId;
    [userDefaults setInteger:self.curUser.cacheUserId forKey:@"cacheUserId"];




    [userDefaults synchronize];
}

- (void)loginOutCurrentAccount {
    self.curUser.userId = -1;
    self.curUser.cacheUserId = self.curUser.userId;

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:@"account"];
    [userDefaults removeObjectForKey:@"pwd"];
    [userDefaults removeObjectForKey:@"openId"];
    [userDefaults removeObjectForKey:@"accessToken"];
    [userDefaults removeObjectForKey:@"isAutoLogin"];

    [userDefaults setInteger:self.curUser.cacheUserId forKey:@"cacheUserId"];

    [userDefaults synchronize];
}

/**
 *  用户是否设置了自动登陆
 *
 *  @return 否设置了自动登陆
 */
- (BOOL) isAutoLogin{
  NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
//  return [userDefaults boolForKey:@"isAutoLogin"];
    return YES;
}

- (BOOL) isALastLoginByQQ{
    NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
    NSString* openId = [userDefaults stringForKey:@"openId"];
    if(openId && openId.length !=0){
        return YES;
    }
    return NO;
}
@end
